<?php

namespace App\Http\Controllers\Home;

use Auth;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Models\Article;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //显示文章列表
        $article = new Article();
        $result = $article::orderBy('created_at','desc')->paginate(4);
        //var_dump($result);
        return view('homes.article.index',['data' => $result]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //新增文章页面
        $result = array('mode' => 'create');
        return view('homes.article.form',['data' => $result]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //新增文章方法
        $article = new Article();
        $article->name = $request->input('name');
        $article->description = $request->input('description');
        $result = $article->save();
        if($result){
            return redirect('/article');
        }else{
            return redirect('/article/create');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //显示文章详情
        $article = new Article();
        $result = $article::where('id',(int)$id)->first();
        return view('homes.article.show',['data' => $result]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id = 0)
    {
        
        //修改文章页面
        $article = new Article();
        $result = $article::where('id',(int)$id)->first();
        $result['mode'] = 'edit';
        return view('homes.article.form',['data' => $result]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //修改文章方法
        $article = new Article();
        $article = $article::where('id',(int)$id)->first();
        $article->name = $request->input('name');
        $article->description = $request->input('description');
        $result = $article->save();
        if($result){
            return redirect('/article');
        }else{
            return redirect('/article/edit-'.$id.'.html');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id = 0)
    {
        //删除文章方法
        $article = new Article();
        $article::findOrFail((int)$id)->delete();
        return redirect('/article');
    }
}
